package com.pks.datacenter.service.api;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pks.datacenter.config.RedisKeyConfig;
import com.pks.datacenter.entity.api.ApiParams;
import com.pks.datacenter.handler.RedisHandler;
import com.pks.datacenter.mapper.api.ApiParamsMapper;
import com.pks.resultful.common.SRemoteResult;
import com.pks.util.SUtilQbuilder;
import com.pks.web.mybatis.service.BasicServiceImpl;

@Component("apiParamsService")
public class ApiParamsServiceImpl extends BasicServiceImpl<ApiParams, Long, ApiParamsMapper> implements ApiParamsService {

	@Autowired
	private ApiParamsMapper apiParamsMapper;
	@Autowired
	private RedisHandler redisHandler;

	@Override
	protected ApiParamsMapper getMapper() {
		return apiParamsMapper;
	}

	@Override
	public SRemoteResult<String> insertOrModify(ApiParams obj) {
		if (obj.getApiId() == null ) {
			return SRemoteResult.initErrorParamBlank("接口ID");
		}
		if (obj.getDbId() == null) {
			return SRemoteResult.initErrorParamBlank("数据源ID");
		}
		if (obj.getDbFieldId() == null) {
			return SRemoteResult.initErrorParamBlank("数据源字段ID");
		}
		if (StringUtils.isBlank(obj.getParamName())) {
			return SRemoteResult.initErrorParamBlank("参数名");
		}
		if (obj.getParamType() == null) {
			return SRemoteResult.initErrorParamBlank("参数类型");
		}
		if (obj.getCompareType() == null) {
			return SRemoteResult.initErrorParamBlank("比较类型");
		}
		if (obj.getRequiredState() == null) {
			obj.setRequiredState(0);
		}
		if (obj.getId() == null) {
			obj.insertBefore();
			apiParamsMapper.insert(obj);
		} else {
			ApiParams update = apiParamsMapper.findById(obj.getId());
			if (update == null) {
				return SRemoteResult.initErrorDataNone();
			}
			apiParamsMapper.update(obj);
		}
		redisHandler.del(RedisKeyConfig.KEY_LIST_APIPARAMS_PREFIX + obj.getApiId());
		return SRemoteResult.initSuccess(null);
	}

	@Override
	public SRemoteResult<String> deleteApiParams(List<Long> ids) {
		if (ids != null && ids.size() > 0) {
			List<ApiParams> list = apiParamsMapper.find(SUtilQbuilder.initQbuilder("ids", ids), null, null, null);
			if (list.size() > 0) {
				if (ids.size() == 1) {
					apiParamsMapper.delete(ids.get(0));
				} else {
					apiParamsMapper.deleteByIds(ids);
				}
				Set<Long> apiIds = new HashSet<Long>();
				for (ApiParams apiParams : list) {
					apiIds.add(apiParams.getApiId());
				}
				for (Long apiId : apiIds) {
					redisHandler.del(RedisKeyConfig.KEY_LIST_APIPARAMS_PREFIX + apiId);
				}
			}
		}
		return SRemoteResult.initSuccess(null);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ApiParams> findByApiId(Long apiId) {
		List<ApiParams> list = new ArrayList<ApiParams>();
		if (apiId != null) {
			Object obj = redisHandler.get(RedisKeyConfig.KEY_LIST_APIPARAMS_PREFIX + apiId);
			if (obj != null) {
				list = (List<ApiParams>) obj;
			} else {
				list = apiParamsMapper.find(SUtilQbuilder.initQbuilder("apiId", apiId), null, null, null);
				redisHandler.set(RedisKeyConfig.KEY_LIST_APIPARAMS_PREFIX + apiId, list , RedisKeyConfig.CACHE_TIME);
			}
		}
		return list;
	}
	
}
